package com.ss.android.article.base.feature.e;

import android.content.Context;
import android.view.ViewGroup;

import com.bytedance.common.utility.Logger;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.detail2.DetailPageListener;
import com.ss.android.article.base.feature.model.Article;
import com.ss.android.article.base.feature.model.CellRef;
import com.ss.android.article.base.feature.video.IMediaCallback;
import com.ss.android.article.base.feature.video.IMediaLayout;
import com.ss.android.article.base.feature.video.IVideoController;
import com.ss.android.article.base.feature.video.ThirdVideoPartnerData;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.model.BaseArticle;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.ref.WeakReference;
import java.util.LinkedList;
import java.util.Queue;

public class VideoAutoPlayHelperDetail {
    public boolean a;
    public boolean b;
    public int c;
    public boolean d;
    public WeakReference<ArticleShareHelper> mArticleShareHelperRef;
    public Queue f;
    public Queue g;
    private static int h = 25;
    private int i;
    private WeakReference<DetailPageListener> mDetailPageListenerRef;
    private static VideoAutoPlayHelperDetail sInstance;
    private Article l;
    private int m;
    private Article mNextVideoArticle;
    private boolean o;

    private VideoAutoPlayHelperDetail() {
        this.a = true;
        this.b = false;
        this.c = 0;
        this.i = 0;
        this.m = 0;
        this.d = false;
        this.o = false;
        this.f = new LinkedList();
        this.g = new LinkedList();
    }

    public final void a(Article arg1, int arg2) {
        this.l = arg1;
        this.m = arg2;
    }

    public final void a(boolean arg3) {
        this.b = arg3;
        if (!this.b) {
            this.c = 0;
            this.i = 0;
            this.f.clear();
        }
    }

    public final boolean a(Context context, String arg12, BaseArticle arg13, long arg14, JSONObject arg16) {
        JSONObject v8;
        long v4 = 0;
        boolean v0 = false;
        if (context != null && (arg13 == null || (this.g.contains(Long.valueOf(arg13.groupId)))) && (this.b)) {
            long v2 = arg13 != null ? arg13.itemId : v4;
            if (arg13 != null) {
                int v0_1 = arg13.aggrType;
            }

            if (arg16 == null) {
                v8 = new JSONObject();
            } else {
                v8 = arg16;
            }

            try {
                if (!v8.has("item_id")) {
                    v8.put("item_id", v2);
                }

                if (!v8.has("aggr_type")) {
                    v8.put("aggr_type", 0);
                }

                if (v8.has("page_type")) {
                } else {
                    v8.put("page_type", "video");
                }
            } catch (Exception v0_2) {
            }

            String v2_1 = "stay_page_auto";
            if (arg13 != null) {
                v4 = arg13.groupId;
            }

            onEvent(context, v2_1, arg12, v4, arg14, v8);
            v0 = true;
        }

        return v0;
    }

    public static void a(IVideoController videoController) {
        if (videoController != null) {
            videoController.getMediaViewLayout();
        }
    }

    public final boolean a(Context context) {
        boolean v0 = false;
        if (context != null) {
            onEvent(context, "auto_play_stop", "detail_no_related", ((long) this.c), 0, null);
            this.c = 0;
            this.i = 0;
            this.f.clear();
            v0 = true;
        }

        return v0;
    }

    public static VideoAutoPlayHelperDetail getInstance() {
        if (sInstance == null) {
            synchronized (VideoAutoPlayHelperDetail.class) {
                if (sInstance == null) {
                    sInstance = new VideoAutoPlayHelperDetail();
                }
            }
        }
        return sInstance;
    }

    public static void onEvent(Context context, String arg2, String arg3, long arg4, long arg6, JSONObject arg8) {
        if (arg8 == null) {
            MobClickCombiner.onEvent(context, arg2, arg3, arg4, arg6);
        } else {
            MobClickCombiner.onEvent(context, arg2, arg3, arg4, arg6, arg8);
        }
    }

    public static void dismissVolumeOrTouchView(IMediaLayout mediaLayout) {
        if (mediaLayout != null) {
            mediaLayout.dismissVolume();
            mediaLayout.dismissTouchProgress();
        }
    }

    public final void setDetailPageListener(DetailPageListener detailPageListener) {
        this.mDetailPageListenerRef = new WeakReference(detailPageListener);
    }

    public final void a(IMediaCallback mediaCallback) {
        if (AppData.getInstance().getAbSettings().isDetailAutoPlayNext() && mediaCallback != null) {
            DetailPageListener detailPageListener = this.mDetailPageListenerRef == null ? null : this.mDetailPageListenerRef.get();
            if (detailPageListener == null) {
                return;
            }
            ++this.c;
            ++this.i;
            this.b = true;
            detailPageListener.b(this.mNextVideoArticle);
            if (this.mNextVideoArticle != null) {
                if (this.f.size() >= h) {
                    this.f.poll();
                }
                this.f.offer(Long.valueOf(this.mNextVideoArticle.groupId));
            }
            this.g.addAll(this.f);
        }
    }

    public final void a(IMediaLayout mediaLayout, boolean arg3, ViewGroup arg4) {
        if (!this.a && mediaLayout != null && !arg3) {
            mediaLayout.setDetailAutoPlayKeepFullScreen(true);
            mediaLayout.enterFullScreen(arg4);
        }
    }

    public final void a(IVideoController videoController, Context context, boolean arg7) {
        IMediaLayout v0_1;
        if (videoController != null) {
            this.mNextVideoArticle = null;
            if (this.o) {
                DetailPageListener detailPageListener = this.mDetailPageListenerRef == null ? null : this.mDetailPageListenerRef.get();
                if (detailPageListener == null) {
                    return;
                }

                this.mNextVideoArticle = ((DetailPageListener) detailPageListener).c();
                if (this.mNextVideoArticle != null) {
                    v0_1 = videoController.getMediaViewLayout();
                    if (this.l == null) {
                        return;
                    }

                    if (this.mNextVideoArticle == null) {
                        return;
                    }

                    if (v0_1 == null) {
                        return;
                    }

                    if (!this.a) {
                        v0_1.showEndCover(((CellRef) null), this.mNextVideoArticle);
                        this.d = false;
                        return;
                    }

                    if (arg7) {
                        return;
                    }

                    if (!AppData.getInstance().getAbSettings().isDetailAutoPlayNext()) {
                        return;
                    }

                    v0_1.showEndCover(((CellRef) null), this.mNextVideoArticle);
                    this.d = false;
                    return;
                }

                if (videoController.isFullScreen()) {
                    videoController.handleFullScreenBackClick(null, null, false);
                }

                v0_1 = videoController.getMediaViewLayout();
                if (v0_1 == null) {
                    return;
                }

                v0_1.showNewCover(this.l);
            } else {
                if (AppData.getInstance().getAbSettings().isDetailAutoPlayNext()) {
                    this.b(context);
                }
            }
        }
    }

    public final void a(boolean arg2, boolean arg3, boolean arg4, boolean arg5) {
        this.a = !AppData.getInstance().getAbSettings().isDetailAutoPlayNext()
                || !AppData.getInstance().getAbSettings().isFullScreenAutoPlayNext()
                || !arg2
                || (arg3)
                || (arg4)
                || (arg5);
    }

    public final boolean onEvent(Context context, String arg13, long arg14, long arg16, JSONObject jsonObject) {
        if (context == null) {
            return false;
        }

        if (this.b) {
            try {
                jsonObject.put("version_type", "high");
            } catch (JSONException jsonException) {
            }
            onEvent(context, "video_over_auto", arg13, arg14, arg16, jsonObject);
            return true;
        } else {
            return false;
        }
    }

    public final boolean reportFullscreenChanged(Context context, boolean isFullscreen) {
        long v6 = 0;
        JSONObject v8 = null;
        boolean v0 = false;
        if (context != null && AppData.getInstance().getAbSettings().isDetailAutoPlayNext() && AppData.getInstance().getAbSettings().isFullScreenAutoPlayNext() && (this.b) && this.i != 0) {
            if (isFullscreen) {
                if (this.m == 1) {
                    onEvent(context, "toggle_fullscreen", "album", ((long) this.i), v6, v8);
                } else {
                    onEvent(context, "toggle_fullscreen", "detail", ((long) this.i), v6, v8);
                }

                this.i = 0;
            } else {
                if (this.m == 1) {
                    onEvent(context, "toggle_fullscreen_exit", "album", ((long) this.i), v6, v8);
                } else {
                    onEvent(context, "toggle_fullscreen_exit", "detail", ((long) this.i), v6, v8);
                }

                this.i = 0;
            }

            v0 = true;
        }

        return v0;
    }

    public final boolean a(Context context, boolean arg11, boolean arg12) {
        boolean v0 = false;
        if (context != null) {
            JSONObject v8 = new JSONObject();
            try {
                if (arg12) {
                    v8.put("auto_type", "auto_play");
                } else {
                    v8.put("auto_type", "unauto_play");
                }

                if (arg11) {
                    v8.put("position", "fullscreen");
                } else {
                    v8.put("position", "detail");
                }
            } catch (JSONException jsonException) {
                return v0;
            }
            onEvent(context, "auto_next", "show_related", this.mNextVideoArticle.groupId, 0, v8);
            return true;
        }

        return v0;
    }

    public final boolean reportNextPlay(Context context, boolean isFullscreen, boolean isPlayRelated, boolean isAutoPlay) {
        if (context != null) {
            if (this.mNextVideoArticle == null) {
                if (!android.support.design.a.isTest(context)) {
                    return false;
                }
                Throwable throwable = new Throwable();
                StringBuffer stringBuffer = new StringBuffer();
                StackTraceElement[] elements = throwable.getStackTrace();
                int length = elements.length;
                for (int i = 0; i < length; ++i) {
                    stringBuffer.append(elements[i].toString());
                }
                Logger.d("NullNextVideo", stringBuffer.toString());
                return false;
            }
            try {
                JSONObject jsonObject = new JSONObject();
                if (isFullscreen) {
                    jsonObject.put("position", "fullscreen");
                } else {
                    jsonObject.put("position", "detail");
                }

                if (isPlayRelated) {
                    onEvent(context, "auto_next", "play_related", this.mNextVideoArticle.groupId, 0, jsonObject);
                } else {
                    if (isAutoPlay) {
                        jsonObject.put("auto_type", "auto_play");
                    } else {
                        jsonObject.put("auto_type", "unauto_play");
                    }
                    onEvent(context, "auto_next", "click_related", this.mNextVideoArticle.groupId, 0, jsonObject);
                }
            } catch (JSONException jsonException) {
                return false;
            }
            return true;
        } else {
            return false;
        }
    }

    public final boolean a(boolean arg3, boolean arg4, ThirdVideoPartnerData thirdVideoPartnerData) {
        boolean v0 = false;
        if (!arg4 && thirdVideoPartnerData == null) {
            if (!arg3
                    || !AppData.getInstance().getAbSettings().isFullScreenAutoPlayNext()
                    || !AppData.getInstance().getAbSettings().isDetailAutoPlayNext()) {
                if (!arg3 && (AppData.getInstance().getAbSettings().isDetailAutoPlayNext())) {
                    this.o = true;
                    return this.o;
                }
                this.o = false;
            } else {
                this.o = true;
                return this.o;
            }
            v0 = this.o;
        }

        return v0;
    }

    public final boolean b(Context context) {
        if (context != null && AppData.getInstance().getAbSettings().isDetailAutoPlayNext() && this.c > 0) {
            onEvent(context, "auto_play_stop", "detail_quit_auto", this.c, 0, null);
            this.c = 0;
            this.i = 0;
            this.f.clear();
            return true;
        }
        return false;
    }

    public final boolean reportCancelPlay(Context context, boolean isFullscreen, boolean isAutoPlay) {
        if (context != null) {
            JSONObject jsonObject = new JSONObject();
            try {
                if (isAutoPlay) {
                    jsonObject.put("auto_type", "auto_play");
                } else {
                    jsonObject.put("auto_type", "unauto_play");
                }
                if (isFullscreen) {
                    jsonObject.put("position", "fullscreen");
                } else {
                    jsonObject.put("position", "detail");
                }

                onEvent(context, "auto_next", "cancel_related", this.mNextVideoArticle.groupId, 0, jsonObject);
                return true;
            } catch (JSONException jsonException) {
                return false;
            }
        }
        return false;
    }
}

